package inc.gome.dynamic.datasource.oss;

/**
 * @author sujizhou
 * @Date 2021/12/20
 * @Desc 类描述信息
 */
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

import com.aliyun.oss.common.auth.HmacSHA256Signature;
import com.aliyun.oss.common.comm.RequestMessage;
import com.aliyun.oss.internal.SignV2Utils;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;


/*os3 签名生成*/
public class OssDemo {
    public static void main(String[] args) throws IOException {
        // 替换为当前集群的信息
        String akey = "4SPIVUVBZ5E3UHM3J05S";
        String skey = "204cvxspRKLMdPeFcKsAF5Px8Wnn5tfVYeMcHoKR ";
        String endpoint = "http://bjgc11.gos.gomedc.com/";

        // 替换为指定的桶名和⽂件名
        String bucketName = "demoapp";
        String stringObjKeyName = "demo1";
        String fileObjKeyName = "/demo_p/demo-cicd/20201010001/xxx.jpg";
        uploadFile(akey, skey, endpoint, bucketName, stringObjKeyName, fileObjKeyName);

/*        //上传文件
        //Canonicalized  规范化

        *//*
        AccessKeySecret表示签名所需的密钥。
        VERB表示HTTP请求方法，例如PUT、GET、DELETE等。
        Content-MD5表示请求内容数据的MD5值，对于图片处理服务这里为空字符串。
        Content-Type表示请求内容的类型，对于图片处理服务这里为空字符串。
        Date表示此次操作的时间，且必须为HTTP1.1中支持的GMT格式。
        CanonicalizedOSSHeaders表示 http中的object meta组合，对于图片处理服务这里为空字符串。
        CanonicalizedResource构造方法请参考上文URL签名中的CanonicalizedResource的生成方法
            /image-demo/example.jpg@100w.jpg
         *//*



   System.out.println(StringToSign);
//        try {
//            String   qm =HmacSHA1.HmacSHA1Encrypt(StringToSign,skey);

//        } catch (Exception e) {
//            e.printStackTrace();
//        }
 */

    }





    private static void uploadFile(String akey, String skey, String endpoint, String bucketName, String stringObjKeyName, String fileObjKeyName) {
        String fileName = "C:\\Users\\sujizhou\\Desktop\\demo\\1.png";
        try {
            AWSCredentials credentials = new BasicAWSCredentials(akey,
                    skey);
            ClientConfiguration clientConfig = new ClientConfiguration();
            clientConfig.setProtocol(Protocol.HTTP);

            AmazonS3 s3Client = new AmazonS3Client(credentials,
                    clientConfig);
            s3Client.setEndpoint(endpoint);



     /*       //创建桶
            Bucket bucket = s3Client.createBucket(bucketName);*/

            // 上传⼀个字符串作为⼀个对象
         //   s3Client.putObject(bucketName, stringObjKeyName, "UploadedString Object");
                    // 上传⼀个⽂件作为⼀个对象
                    PutObjectRequest request = new PutObjectRequest(bucketName,
                            fileObjKeyName, new File(fileName)); // 构造⼀个 Put Object 请求
            ObjectMetadata metadata = new ObjectMetadata(); // 上传对象时写⼊
            metadata.setContentType("plain/text"); // 系统元数据
            metadata.addUserMetadata("x-amz-meta-title", "someTitle"); //自定义元数据
            request.setMetadata(metadata); // 设置对象元数据
           s3Client.putObject(request); // 发送 Put Object 请求
        }
        catch(AmazonServiceException e) {
            // 服务端错误
            e.printStackTrace();
        }
        catch(AmazonClientException e) {
            // 客户端错误
            e.printStackTrace();
        }
    }
}
